﻿Imports OpenHardwareMonitor.Hardware
Imports GPU_Info_Extractor

Public Class frmMain

#Region " Declerations "

	Dim get_GPU_Info_ As New GPU_Info_Extractor.GPUMonitor_Extractor
	Dim MyComputer As New Computer
	Dim mainComputernode As New TreeNode

	Dim rootNode As Ai.Control.TreeNode = New Ai.Control.TreeNode

	Dim GPUName As String = get_GPU_Info_.getFromOpenHardwareMonitor_GPUName.ToString
	Dim nodeGPU As Ai.Control.TreeNode = New Ai.Control.TreeNode(GPUName)

	Dim nodeClock As Ai.Control.TreeNode = New Ai.Control.TreeNode("Clockes")
	Dim nodeLoad As Ai.Control.TreeNode = New Ai.Control.TreeNode("Load")
	Dim nodeTemperature As Ai.Control.TreeNode = New Ai.Control.TreeNode("Temperature")
	Dim nodeFan As Ai.Control.TreeNode = New Ai.Control.TreeNode("Fan")
	Dim nodeControl As Ai.Control.TreeNode = New Ai.Control.TreeNode("Control")

	Dim nodeClock_GPUCore As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Core")
	Dim nodeClock_GPUMemory As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Memory")
	Dim nodeClock_GPUShader As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Shader")
	Dim nodeLoad_GPUCORE As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Core")
	Dim nodeLoad_GPUMemoryController As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Memory Controller")
	Dim nodeLoad_GPUVideoEngine As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Video Engine")
	Dim nodeLoad_GPUMemory As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Memory")
	Dim nodeTemp_GPUCORE As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Core")
	Dim nodeFans_GPU As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU")
	Dim nodeControl_GPUFan As Ai.Control.TreeNode = New Ai.Control.TreeNode("GPU Fan")

	Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

#End Region

#Region " Functions "

	Enum PrograssBarColor
		Green = &H1
		Red = &H2
		Yellow = &H3
	End Enum

	Private Shared Sub ChangePrograssBarColor(ByVal PrograssBar_Name As Windows.Forms.ProgressBar, ByVal PrograssBar_Color As PrograssBarColor)
		Try
			SendMessage(PrograssBar_Name.Handle, &H410, PrograssBar_Color, 0)
		Catch ex As Exception

		End Try
	End Sub

	Public Sub getFromOpenHardwareMonitor_SensorValue(ByVal treelistnodev As Ai.Control.TreeNode, ByVal sensorName As String, ByVal sensorTyp As SensorType)

		Try
			Dim unit As String = String.Empty
			MyComputer.Open()
			MyComputer.GPUEnabled = True

			treelistnodev.SubItems.Clear()

			For Each hardware In MyComputer.Hardware

				If hardware.HardwareType = get_GPU_Info_.getFromOpenHardwareMonitor_GPUType() Then
					hardware.Update()

					For Each sensor In hardware.Sensors
						If sensor.SensorType = SensorType.Clock Then
							unit = "MHz"
						ElseIf sensor.SensorType = SensorType.Load Or sensor.SensorType = SensorType.Control Then
							unit = "%"
						ElseIf sensor.SensorType = SensorType.Temperature Then
							unit = Chr(176) & "C"
						ElseIf sensor.SensorType = SensorType.Fan Then
							unit = " RPM"
						End If

						If sensor.Name = sensorName Then
							If sensor.SensorType = sensorTyp Then
								If sensor.Value IsNot Nothing Then

									treelistnodev.SubItems.Add(Format(CDec(sensor.Value), "Fixed") & " " & unit)

								End If

							End If

						End If

					Next

				End If
			Next
		Catch ex As Exception

		End Try


	End Sub

	Public Sub getFromOpenHardwareMonitor_GPUBrand(ByVal combo As ComboBox)

		Try
			MyComputer.Open()
			MyComputer.GPUEnabled = True

			For Each hardware In MyComputer.Hardware
				If hardware.HardwareType = HardwareType.GpuNvidia Then
					hardware.Update()
					combo.Items.Add(hardware.Name)
					combo.SelectedIndex = 0
					Exit Sub
				ElseIf hardware.HardwareType = HardwareType.GpuNvidia Then
					hardware.Update()
					combo.Items.Add(hardware.Name)
					combo.SelectedIndex = 0
					Exit Sub
				End If
			Next
		Catch ex As Exception
		End Try


	End Sub

	Private Sub loadTheMainApp()
		Try
			If get_GPU_Info_.getFromOpenHardwareMonitor_GPUType = HardwareType.GpuNvidia Then
				picBxGPU.BackgroundImage = My.Resources.nvidea
			ElseIf get_GPU_Info_.getFromOpenHardwareMonitor_GPUType = HardwareType.GpuAti Then
				picBxGPU.BackgroundImage = My.Resources.ATI

			End If
			ComboBox1.SelectedIndex = 0
			ComboBox2.Items.Add("Temperature " & " " & Chr(176) & " C ")
			ComboBox2.SelectedIndex = 0
			ComboBox3.SelectedIndex = 0

			If Environment.Is64BitOperatingSystem Then
				Me.Text = Me.Text & " " & "  64 bit "
			End If

			Me.Icon = My.Resources.GPU
			Me.Opacity = 0
			Me.Show()
			timrStart.Enabled = True
			createNode()

			getFromOpenHardwareMonitor_GPUBrand(ComboGPU)
			timrClocks.Enabled = True
			timrLoad.Enabled = True
			timrTemperature.Enabled = True
			timrFan.Enabled = True
			timrControl.Enabled = True
			btnStart.Enabled = False

		Catch ex As Exception

		End Try
	End Sub

	Private Sub createNode()
		Try
			rootNode.Text = My.Computer.Name

			nodeClock_GPUCore.Name = "nodeClock_GPUCore"
			nodeClock_GPUMemory.Name = "nodeClock_GPUMemory"
			nodeClock_GPUShader.Name = "nodeClock_GPUShader"
			nodeLoad_GPUCORE.Name = "nodeLoad_GPUCORE"
			nodeLoad_GPUMemoryController.Name = "nodeLoad_GPUMemoryController"
			nodeLoad_GPUVideoEngine.Name = "nodeLoad_GPUVideoEngine"
			nodeLoad_GPUMemory.Name = "nodeLoad_GPUMemory"
			nodeTemp_GPUCORE.Name = "nodeTemp_GPUCORE"
			nodeFans_GPU.Name = "nodeFans_GPU"
			nodeControl_GPUFan.Name = "nodeControl_GPUFan"

			nodeClock.Nodes.Add(nodeClock_GPUCore)
			nodeClock.Nodes.Add(nodeClock_GPUMemory)
			nodeClock.Nodes.Add(nodeClock_GPUShader)

			nodeClock.expandAll()

			nodeLoad.Nodes.Add(nodeLoad_GPUCORE)
			nodeLoad.Nodes.Add(nodeLoad_GPUMemoryController)
			nodeLoad.Nodes.Add(nodeLoad_GPUVideoEngine)
			nodeLoad.Nodes.Add(nodeLoad_GPUMemory)

			nodeLoad.expandAll()

			nodeTemperature.Nodes.Add(nodeTemp_GPUCORE)
			nodeFan.Nodes.Add(nodeFans_GPU)
			nodeControl.Nodes.Add(nodeControl_GPUFan)

			nodeTemperature.expandAll()
			nodeFan.expandAll()
			nodeControl.expandAll()

			nodeGPU.Nodes.Add(nodeClock)
			nodeGPU.Nodes.Add(nodeLoad)
			nodeGPU.Nodes.Add(nodeTemperature)
			nodeGPU.Nodes.Add(nodeFan)
			nodeGPU.Nodes.Add(nodeControl)
			nodeGPU.expandAll()
			rootNode.Nodes.Add(nodeGPU)

			If get_GPU_Info_.getFromOpenHardwareMonitor_GPUType = HardwareType.GpuNvidia Then
				nodeGPU.Image = My.Resources.nvidea
			ElseIf get_GPU_Info_.getFromOpenHardwareMonitor_GPUType = HardwareType.GpuAti Then
				nodeGPU.Image = My.Resources.ATI
			End If


			nodeClock.Image = My.Resources.OpenHardwareMonitor_Resources_clock
			nodeLoad.Image = My.Resources.OpenHardwareMonitor_Resources_load
			nodeTemperature.Image = My.Resources.OpenHardwareMonitor_Resources_temperature
			nodeFan.Image = My.Resources.OpenHardwareMonitor_Resources_fan
			nodeControl.Image = My.Resources.OpenHardwareMonitor_Resources_control
			rootNode.Image = My.Resources.OpenHardwareMonitor_Resources_computer
			mcvTree.Nodes.Add(rootNode)
			mcvTree.expandAll()

		Catch ex As Exception

		End Try
	End Sub

	Private Sub cleartreeNode()
		Try
			nodeClock_GPUCore.SubItems.Clear()
			nodeClock_GPUMemory.SubItems.Clear()
			nodeClock_GPUShader.SubItems.Clear()
			nodeLoad_GPUCORE.SubItems.Clear()
			nodeLoad_GPUMemoryController.SubItems.Clear()
			nodeLoad_GPUVideoEngine.SubItems.Clear()
			nodeLoad_GPUMemory.SubItems.Clear()
			nodeTemp_GPUCORE.SubItems.Clear()
			nodeFans_GPU.SubItems.Clear()
			nodeControl_GPUFan.SubItems.Clear()

			nodeClock_GPUCore.SubItems.Add(" ")
			nodeClock_GPUMemory.SubItems.Add(" ")
			nodeClock_GPUShader.SubItems.Add(" ")
			nodeLoad_GPUCORE.SubItems.Add(" ")
			nodeLoad_GPUMemoryController.SubItems.Add(" ")
			nodeLoad_GPUVideoEngine.SubItems.Add(" ")
			nodeLoad_GPUMemory.SubItems.Add(" ")
			nodeTemp_GPUCORE.SubItems.Add(" ")
			nodeFans_GPU.SubItems.Add(" ")
			nodeControl_GPUFan.SubItems.Add(" ")

			lblTemp.Text = " "
			lblLoad.Text = " "
			lblFan.Text = " "
			pBar_Fan.Value = 0
			pBar_Load.Value = 0
			pBar_Temp.Value = 0

		Catch ex As Exception

		End Try
	End Sub

	Private Sub ResetMe()
		Try
			frmSettings.resetStartup_Settings()
			My.Settings.Reset()
			My.Settings.Save()
			Process.Start(MyPath)
		Catch
		End Try
		End

	End Sub

	Private Sub setPrograssBarColors()
		Try
			If ((pBar_Fan.Value / pBar_Fan.Maximum) * 100) >= 75 Then
				ChangePrograssBarColor(pBar_Fan, PrograssBarColor.Red)
			ElseIf ((pBar_Fan.Value / pBar_Fan.Maximum) * 100) >= 50 Then
				ChangePrograssBarColor(pBar_Fan, PrograssBarColor.Yellow)
			Else
				ChangePrograssBarColor(pBar_Fan, PrograssBarColor.Green)
			End If

			If ((pBar_Load.Value / pBar_Load.Maximum) * 100) >= 75 Then
				ChangePrograssBarColor(pBar_Load, PrograssBarColor.Red)
			ElseIf ((pBar_Load.Value / pBar_Load.Maximum) * 100) >= 50 Then
				ChangePrograssBarColor(pBar_Load, PrograssBarColor.Yellow)
			Else
				ChangePrograssBarColor(pBar_Load, PrograssBarColor.Green)
			End If

			If ((pBar_Temp.Value / pBar_Temp.Maximum) * 100) >= 75 Then
				ChangePrograssBarColor(pBar_Temp, PrograssBarColor.Red)
			ElseIf ((pBar_Temp.Value / pBar_Temp.Maximum) * 100) >= 50 Then
				ChangePrograssBarColor(pBar_Temp, PrograssBarColor.Yellow)
			Else
				ChangePrograssBarColor(pBar_Temp, PrograssBarColor.Green)
			End If


		Catch ex As Exception

		End Try



	End Sub

	Private Function MyPath() As String
		Try

			Dim AppLocation As String = Application.StartupPath & "\" & My.Application.Info.AssemblyName & ".exe"

			Return AppLocation
		Catch ex As Exception
			Return Nothing
		End Try
		Return Nothing

	End Function

#End Region

#Region " Form "

	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Try
			Me.Hide()
			loadTheMainApp()
		Catch ex As Exception
			End
		End Try
	End Sub

	Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		Try
			If e.KeyCode = Keys.Escape Then
				btnExit.PerformClick()
			ElseIf e.KeyCode = Keys.F12 And e.Modifiers = Keys.Control Then
				ResetMe()
			End If
		Catch ex As Exception

		End Try
	End Sub

	Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Try

			timrEnd.Enabled = True
			Exit Sub
		Catch ex As Exception

		End Try
	End Sub

#End Region

#Region " GUI "


	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Try

			timrClocks.Enabled = True
			timrLoad.Enabled = True
			timrTemperature.Enabled = True
			timrFan.Enabled = True
			timrControl.Enabled = True
			btnStart.Enabled = False
			btnStop.Enabled = True
		Catch ex As Exception

		End Try
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Try
			timrClocks.Enabled = False
			timrLoad.Enabled = False
			timrTemperature.Enabled = False
			timrFan.Enabled = False
			timrControl.Enabled = False
			btnStart.Enabled = True
			btnStop.Enabled = False
			cleartreeNode()
		Catch ex As Exception

		End Try
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Try

			timrClocks.Enabled = False
			timrLoad.Enabled = False
			timrTemperature.Enabled = False
			timrFan.Enabled = False
			timrControl.Enabled = False
			timrEnd.Enabled = True
		Catch ex As Exception

		End Try
	End Sub

	Private Sub btnNotificationBar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNotificationBar.Click
		Try
			If get_GPU_Info_.ThermalSensorPresent = True Then
				frmTaskBar.Show()
				frmTaskBar.CMS_Start.PerformClick()
				btnStop.PerformClick()
				Me.Close()
			Else
				MsgBox("No GPU thermal sensor was detected", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, " Ops ")
			End If

		Catch ex As Exception

		End Try
	End Sub

	Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
		Try
			frmSettings.ShowDialog()
		Catch ex As Exception

		End Try
	End Sub

#End Region

#Region " Timers "

	Private Sub timrStart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrStart.Tick
		Try
			Me.Opacity = Me.Opacity + 0.1
			If Me.Opacity >= 1 Then
				timrStart.Enabled = False
			End If
		Catch ex As Exception

		End Try
	End Sub

	Private Sub timrEnd_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrEnd.Tick
		Try
			Me.Opacity = Me.Opacity - 0.1
			If Me.Opacity <= 0 Then
				timrStart.Enabled = False
				Me.Close()
			End If
		Catch ex As Exception

		End Try
	End Sub

	Private Sub timrClocks_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrClocks.Tick
		Try

			getFromOpenHardwareMonitor_SensorValue(nodeClock_GPUCore, "GPU Core", SensorType.Clock)
			getFromOpenHardwareMonitor_SensorValue(nodeClock_GPUMemory, "GPU Memory", SensorType.Clock)
			getFromOpenHardwareMonitor_SensorValue(nodeClock_GPUShader, "GPU Shader", SensorType.Clock)

		Catch ex As Exception

		End Try
	End Sub

	Private Sub timrLoad_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrLoad.Tick
		Try
			getFromOpenHardwareMonitor_SensorValue(nodeLoad_GPUCORE, "GPU Core", SensorType.Load)
			getFromOpenHardwareMonitor_SensorValue(nodeLoad_GPUMemoryController, "GPU Memory Controller", SensorType.Load)
			getFromOpenHardwareMonitor_SensorValue(nodeLoad_GPUVideoEngine, "GPU Video Engine", SensorType.Load)
			getFromOpenHardwareMonitor_SensorValue(nodeLoad_GPUMemory, "GPU Memory", SensorType.Load)

			dataChartLoad.UpdateChart(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Memory", SensorType.Load))
			lblLoad.Text = get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Memory", SensorType.Load)


			pBar_Load.Value = CInt(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Memory", SensorType.Load))
			If pBar_Load.Value > pBar_Load.Maximum Then
				pBar_Load.Maximum = pBar_Load.Value
			End If
			setPrograssBarColors()

		Catch ex As Exception

			If pBar_Load.Value > pBar_Load.Maximum Then
				pBar_Load.Maximum = pBar_Load.Value
			End If
		End Try
	End Sub

	Private Sub timrTemperature_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrTemperature.Tick
		Try
			getFromOpenHardwareMonitor_SensorValue(nodeTemp_GPUCORE, "GPU Core", SensorType.Temperature)

			dataChartTemperature.UpdateChart(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Core", SensorType.Temperature))
			lblTemp.Text = get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Core", SensorType.Temperature)

			pBar_Temp.Value = CInt(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Core", SensorType.Temperature))
			If pBar_Temp.Value > pBar_Temp.Maximum Then
				pBar_Temp.Maximum = pBar_Temp.Value
			End If

		Catch ex As Exception
			If pBar_Temp.Value > pBar_Temp.Maximum Then
				pBar_Temp.Maximum = pBar_Temp.Value
			End If
		End Try
	End Sub

	Private Sub timrFan_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrFan.Tick
		Try
			getFromOpenHardwareMonitor_SensorValue(nodeFans_GPU, "GPU", SensorType.Fan)


			dataChartFan.UpdateChart(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU", SensorType.Fan))
			lblFan.Text = get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU", SensorType.Fan)

			pBar_Fan.Value = CInt(get_GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU", SensorType.Fan))
			If pBar_Fan.Value > pBar_Fan.Maximum Then
				pBar_Fan.Maximum = pBar_Fan.Value
			End If
		Catch ex As Exception
			If pBar_Fan.Value > pBar_Fan.Maximum Then
				pBar_Fan.Maximum = pBar_Fan.Value
			End If
		End Try
	End Sub

	Private Sub timrControl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrControl.Tick
		Try
			getFromOpenHardwareMonitor_SensorValue(nodeControl_GPUFan, "GPU Fan", SensorType.Control)

		Catch ex As Exception

		End Try
	End Sub

#End Region

End Class
